package com.zx.wangyixinwen.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zx.wangyixinwen.MyRequestQueue;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.activity.NewsDetailsActivity;
import com.zx.wangyixinwen.adapter.HeadlinesListAdapter;
import com.zx.wangyixinwen.adapter.HeadlinesViewPagerAdapter;
import com.zx.wangyixinwen.bean.HeadlinesDetals;
import com.zx.wangyixinwen.bean.News;
import com.zx.wangyixinwen.refresh.SwipeRefreshLoadingLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/6.
 */
public class HeadlinesFragment extends Fragment  {
    private ListView listView;
    private HeadlinesListAdapter adapter;
    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;
    private List<HeadlinesDetals> datas;
    private HeadlinesViewPagerAdapter pagerAdapter;
    private Context context;
    private List<News> news;
    private RequestQueue queue;

        private String url = "http://c.3g.163.com/nc/article/headline/T1348647909107/0-20.html";
    private ViewPager viewPager;

    private int[] imageviews = {R.mipmap.yanmeia, R.mipmap.yanmei3,
            R.mipmap.yanmei4, R.mipmap.yanmei5};
    private List<View> views;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_headlines, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // head
        listView = (ListView) view.findViewById(R.id.listView_headlines);
//        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.head_swipeRefresh);
//        swipeRefreshLoadingLayout.setOnRefreshListener(this);
//        refreshLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.refresh_headlines);
        views = new ArrayList<>();

        headData();
//        refreshLoadingLayout.setOnLoadListener(this);
//        refreshLoadingLayout.setOnRefreshListener(this);


//        // 队列初始化
//        queue = Volley.newRequestQueue(context);
//        final StringRequest stringRequest = new StringRequest(url,
//                new Response.Listener<String>() {
//                    @Override
//                    public void onResponse(String response) {
//                        Gson gson = new Gson();
//                        datas = new ArrayList<>();
//                        Infor infor = gson.fromJson(response, Infor.class);
//                        for (int i = 0; i < infor.getT1348647909107().size(); i++) {
//                            HeadlinesDetals headlines = new HeadlinesDetals();
//                            headlines.setTitle(infor.getT1348647909107().get(i).getTitle());
//                            headlines.setBody(infor.getT1348647909107().get(i).getDigest());
//                            headlines.setPicture(infor.getT1348647909107().get(i).getImgsrc());
//                            datas.add(headlines);
//                        }
//                        adapter = new HeadlinesListAdapter(context, datas);
//                        listView.setAdapter(adapter);
//                        addHeader(infor);
//                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                            @Override
//                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                                Intent intent = new Intent(getActivity(), NewsDetailsActivity.class);
//                                startActivity(intent);
//                            }
//                        });
//                    }
//                }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//        queue.add(stringRequest);

    }

    private void headData() {
        // 队列初始化
        final List<News> list = new ArrayList<>();
        queue = Volley.newRequestQueue(context);
        final StringRequest stringRequest = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.e("-----", response);
                        Gson gson = new Gson();
                        datas = new ArrayList<>();
                        News infor = gson.fromJson(response, News.class);
                        for (int i = 0; i < infor.getT1348647909107().size(); i++) {
                            HeadlinesDetals headlines = new HeadlinesDetals();
                            headlines.setTitle(infor.getT1348647909107().get(i).getTitle());
                            headlines.setBody(infor.getT1348647909107().get(i).getDigest());
                            headlines.setPicture(infor.getT1348647909107().get(i).getImgsrc());
                            datas.add(headlines);
                        }
                        list.add(infor);
                        adapter = new HeadlinesListAdapter(context, datas, list);
                        listView.setAdapter(adapter);
                        addHeader(infor);

                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                Intent intent = new Intent(getActivity(), NewsDetailsActivity.class);
                                intent.putExtra("puttong",list.get(0).getT1348647909107().get(position).getUrl_3w());
                                startActivity(intent);
                            }
                        });
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(stringRequest);
    }

    private void addHeader(News infor) {
        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.headlines_vipager, null);
        viewPager = (ViewPager) headView.findViewById(R.id.headlines_viewpager);
        for (int i = 0; i < imageviews.length; i++) {
            View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.item_iv_headlines_viewpager_head, null);
            NetworkImageView iv_head = (NetworkImageView) view1.findViewById(R.id.item_iv_head);
            MyRequestQueue.setNetImage(iv_head, infor.getT1348647909107().get(0).getAds().get(i).getImgsrc());
            views.add(view1);
        }
        pagerAdapter = new HeadlinesViewPagerAdapter(views, context);
        viewPager.setAdapter(pagerAdapter);
        listView.addHeaderView(headView);
        viewPager.setCurrentItem(Integer.MAX_VALUE / 2);
    }
//
//    @Override
//    public void onRefresh() {
//        new Handler().post(new Runnable() {
//            @Override
//            public void run() {
//                swipeRefreshLoadingLayout.setRefreshing(false);
//                headData();
//            }
//        });
//    }


//    @Override
//    public void onLoad() {
//
//    }
//
//
//    @Override
//    public void onRefresh() {
//
//        adapter.refreshDatas(news);
//        refreshLoadingLayout.setRefreshing(false);
//    }
}
